/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.asiainfo.redis.web.controller;

import com.asiainfo.redis.entity.BusiDataRule;
import com.asiainfo.redis.entity.BusiDatasource;
import com.asiainfo.redis.entity.Result;
import com.asiainfo.redis.entity.SaveBusiTypesRequest;
import com.asiainfo.redis.service.BusiConfigService;
import com.asiainfo.redis.web.PageData;
import com.asiainfo.redis.web.PageParameter;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.URLDecoder;

/**
 * @version v 1.0 on 2016/6/22 10:22
 * @auther william.xu
 */
@RestController
@RequestMapping("/busi-config")
public class BusiConfigController extends BaseController{

    @Resource
    private BusiConfigService busiConfigService;

    @RequestMapping("/getAllBusiTypeList")
    public Result getAllBusiTypeList(){
        Result result = new Result();
        result.setResult(busiConfigService.getAllBusiTypeList());
        return result;
    }

    @RequestMapping("/saveBusiTypes")
    public Result saveBusiTypes(@RequestBody SaveBusiTypesRequest request){
        Result result = new Result();
        try {
            busiConfigService.saveBusiTypes(request);
        }catch (DataIntegrityViolationException e){
            result.setSuccess(false);
            result.setErrorMsg("业务编码存在重复，保存失败！");
        }
        return result;
    }

    @RequestMapping("/removeBusiType")
    public Result removeBusiType(Long id) throws Exception{
        Result result = new Result();
        busiConfigService.removeBusiType(id);
        return result;
    }

    @RequestMapping("/getAllBusiDatasourceList")
    public Result getAllBusiDatasourceList(){
        Result result = new Result();
        result.setResult(busiConfigService.getAllBusiDatasourceList());
        return result;
    }

    @RequestMapping("/saveBusiDatasource")
    public Result saveBusiDatasource(BusiDatasource busiDatasource) throws Exception{
        Result result = new Result();
        busiConfigService.saveBusiDatasource(busiDatasource);
        return result;
    }

    @RequestMapping("/removeBusiDatasource")
    public Result removeBusiDatasource(Long id) throws Exception{
        Result result = new Result();
        busiConfigService.removeBusiDatasource(id);
        return result;
    }

    @RequestMapping("/checkBusiDatasource")
    public Result checkBusiDatasource(BusiDatasource busiDatasource){
        return busiConfigService.checkDatasource(busiDatasource);
    }

    @RequestMapping("/getSqlFields")
    public Result getSqlFields(Long dataSourceId, String sql) throws Exception{
        Result result = new Result();
        result.setResult(busiConfigService.getSqlFields(dataSourceId, sql));
        return result;
    }

    @RequestMapping("/getBusiDataRule")
    public Result getBusiDataRule(@RequestParam(value = "busiDataRuleId", required = true)Long id) throws
            Exception{
        Result result = new Result();
        result.setResult(busiConfigService.getBusiDataRule(id));
        return result;
    }

    @RequestMapping("/getBusiDataInfo")
    public Result getBusiDataInfo(@RequestParam(value = "busiDataRuleId", required = true)Long id) throws
            Exception{
        Result result = new Result();
        result.setResult(busiConfigService.getBusiDataInfo(id));
        return result;
    }


    @RequestMapping("/saveBusiDataRule")
    public Result saveBusiDataRule(BusiDataRule busiDataRule) throws Exception{
        Result result = new Result();
        busiConfigService.saveBusiDataRule(busiDataRule);
        return result;
    }


    @RequestMapping("/getBusiDataRuleList")
    public PageData getBusiDataRuleList(@RequestParam(value = "configName", required = false) String configName,
                                        @RequestParam(value = "keyPrefix", required = false) String keyPrefix,
                                        @RequestParam(value = "busiTypeId", required = true) Long busiTypeId,
                                        @RequestParam(value = "clusterId", required = true) Long clusterId,
                                        PageParameter pageParameter) {
        Page data = busiConfigService.getBusiDataRuleList(configName, keyPrefix, busiTypeId, clusterId, pageParameter
                .getPageable());
        return new PageData(data);
    }

    @RequestMapping("/getSaveFields")
    public Result getSaveFields(@RequestParam(value = "busiDataRuleId", required = true)Long busiDataRuleId){
        Result result = new Result();
        result.setResult(busiConfigService.getSaveFields(busiDataRuleId));
        return result;
    }

    @RequestMapping("/getDatabasePageData")
    public PageData getDatabasePageData(@RequestParam(value = "busiDataRuleId", required = true) Long busiDataRuleId,
                                        String sqlCondition, PageParameter pageParameter) throws Exception{
        return busiConfigService.getDatabasePageData(busiDataRuleId, sqlCondition, pageParameter.getPageable());
    }

    @RequestMapping("/getRedisKeys")
    public Result getRedisKeys(@RequestParam(value = "busiDataRuleId", required = true) Long busiDataRuleId,
                                 @RequestParam(value = "row", required = true)String row) throws Exception{
        Result result = new Result();
        result.setResult(busiConfigService.getRedisKeys(busiDataRuleId, encodedString2Map(row)));
        return result;
    }

    @RequestMapping("/getRedisValueByKey")
    public Result getRedisValueByKey(@RequestParam(value = "busiDataRuleId", required = true) Long busiDataRuleId,
                               @RequestParam(value = "key", required = true)String key) throws Exception{
        Result result = new Result();
        result.setResult(busiConfigService.getRedisValueByKey(busiDataRuleId, key));
        return result;
    }

    @RequestMapping("/setRedisValueByKey")
    public Result setRedisValueByKey(@RequestParam(value = "busiDataRuleId", required = true) Long busiDataRuleId,
                                     @RequestParam(value = "key", required = true)String key,
                                     @RequestParam(value = "value", required = true)String value) throws Exception{
        Result result = new Result();
        busiConfigService.setRedisValueByKey(busiDataRuleId, key, encodedString2Map(value));
        return result;
    }

    @RequestMapping("/writeDataToRedis")
    public Result writeDataToRedis(@RequestParam(value = "busiDataRuleId", required = true)Long id,
                                   String sqlCondition, Integer operFlag) throws
            Exception{
        Result result = new Result();
        busiConfigService.writeDataToRedis(id, sqlCondition, operFlag);
        return result;
    }

}
